import { useReducer } from "react"
import { configureStore, createSlice } from '@reduxjs/toolkit'

export const infoKey = 'infoKey_user_8988'

const useHashRouter_key = 'useHashRouter_key'
// 导航封装
export let useHashRouter = localStorage.getItem(useHashRouter_key) === 'true' ?? true// 是否使用HashRouter，否则使用BrowserRouter
export function changeRouterMode(isHash = true) {
  localStorage.setItem(useHashRouter_key, isHash)
  // useHashRouter = isHash
  // go()
  window.location.href = window.location.origin
}
export function go(href) {  // 跳转到应用内指定页面
  window.location.href = go_(href)
}
export function go_(href = '') {// 生成应用内指定页面的链接
  const url = useHashRouter? ("#/" + href): ("/" + href)
  return url.replace('//', '/')
}
export function checkLocation(href) {
  if (useHashRouter) {
    const hashOrPathName = window.location.hash
    if (hashOrPathName) {
      return hashOrPathName === go_(href)
    }
    return '#/' === go_(href)
  } else {
    return go_(window.location.pathname) === go_(href)
  }
}



export function useMyReducer(initialState) {
  return useReducer((state, payload) => ({...state, ...payload}), initialState)
}














export const counterSlice = createSlice({
  name: 'counter',
  initialState: {
    value: 0,
  },
  reducers: {
    increment: (state) => {
      // Redux Toolkit allows us to write "mutating" logic in reducers. It
      // doesn't actually mutate the state because it uses the Immer library,
      // which detects changes to a "draft state" and produces a brand new
      // immutable state based off those changes
      state.value += 1
    },
    decrement: (state) => {
      state.value -= 1
    },
    incrementByAmount: (state, action) => {
      state.value += action.payload
    },
  },
})

// Action creators are generated for each case reducer function
export const { increment, decrement, incrementByAmount } = counterSlice.actions

export const store = configureStore({
  reducer: {
    counter: counterSlice.reducer
  },
})